package com.jd;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.print.Doc;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
import java.util.List;

public class Student {
    /*
      删除ID为 1的成绩
      修改ID为2的成绩为60
      添加一个学员的成绩信息(ID为3)
     */
    @Test
    public void testDeleteId()throws Exception{
        SAXReader reader=new SAXReader();
        Document doc=reader.read("src/main/resources/student.xml");

        List list=doc.getRootElement().elements("student");
        Element studentEl=(Element) list.get(0); //拿到第一个节点

       //拿到父节点在删除进行删除子节点，这样删父节点也删除了，
        // 如果只删除里面的子节点，就要把子节点全部取出来，在访问到父节点，从而删除子节点，具体看Test类操作
        studentEl.getParent().remove(studentEl);

        //保存文件
        OutputFormat format=OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter writer=new XMLWriter(new FileOutputStream("src/main/resources/student.xml"));
        writer.write(doc);
        writer.close();
    }


    //修改ID为2的成绩为60
    @Test
    public void testUpdate()throws Exception{
       SAXReader reader=new SAXReader();
        Document doc=reader.read("src/main/resources/student.xml");
        List list=doc.getRootElement().elements("student");
        Element student1= (Element) list.get(1);

        //获取第二个student标签下的所有子节点
        List attributes=student1.elements();
        Element e=(Element) attributes.get(2);
        //更新文本内容
        e.setText("60");

        OutputFormat format=OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter writer=new XMLWriter(new FileOutputStream("src/main/resources/student.xml"),format);
        writer.write(doc);
        writer.close();
    }


    //添加一个学员的成绩信息(ID为3)
    @Test
    public void addStudnetNode()throws Exception{
      SAXReader reader=new SAXReader();
      Document doc=reader.read("src/main/resources/student.xml");
      Element scorceEl=doc.getRootElement();
      List list=scorceEl.elements();

      //创建新节点
        Element studentEl= DocumentHelper.createElement("student").addAttribute("id","3");
        Element studentEl2= DocumentHelper.createElement("name");
        studentEl2.setText("测试");
        Element studentEl3= DocumentHelper.createElement("course");
        studentEl3.setText("Java框架开发基础");
        Element studentEl4= DocumentHelper.createElement("score");
        studentEl4.setText("100");

       //添加父子关系   =========>>不明所以，不起作用
//        studentEl.appendAttributes(studentEl2);
//        studentEl.appendAttributes(studentEl3);
//        studentEl.appendAttributes(studentEl4);
        list.add(1,studentEl);
        list.add(2,studentEl2);
        list.add(3,studentEl3);
        list.add(4,studentEl4);

        //保存文件
        OutputFormat format=OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter writer=new XMLWriter(new FileOutputStream("src/main/resources/student.xml"));
        writer.write(doc);
        writer.close();
    }


}
